package com.vtech.ws.gis.impl;

import com.vtech.ws.commons.FileEntity;
import com.vtech.ws.commons.FileUploadHandler;
import com.vtech.ws.gis.TauDEMService;
import org.apache.commons.exec.CommandLine;
import org.egc.commons.command.CommonsExec;
import org.springframework.stereotype.Service;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import java.io.File;
import java.io.IOException;

/**
 * TODO
 *
 * @author houzhiwei
 * @date 2018/9/11 22:07
 */
@Service
public class TauDEMServiceImpl implements TauDEMService {
    @Override
    public FileEntity pitRemove(FileEntity dem) {

        String saveDir = "";

        try {
            String demFile = FileUploadHandler.upload(dem, saveDir);
            CommandLine commandLine = new CommandLine("mpiexec -n 8 PitRemove -z");

            commandLine.addArgument(demFile, true);
            commandLine.addArgument("-fel");
            String outputfile = saveDir + File.separator + dem.getName() + "fel.tif";
            commandLine.addArgument(outputfile);
            String out = (String) CommonsExec.execWithOutput(commandLine).get("out");
            System.out.println(out);
            FileEntity filled = new FileEntity();
            filled.setExtension("tif");
            filled.setFile(new DataHandler(new FileDataSource(new File(outputfile))));
            filled.setName(dem.getName() + "fel");
            return filled;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("failed");
            return null;
        }
    }
}
